<template>
  <div>
    <el-dialog
      :visible.sync="dialog.add"
      :close-on-click-modal="false"
      title="添加"
      width="80%"
      append-to-body
      @closed="dialogClosed('addForm')"
    >
      <el-form ref="addForm" :model="form" label-width="120px" :rules="validateRules">
        <el-row>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="店铺标题" prop="shopTitle">
              <el-input v-model="form.shopTitle" placeholder="请输入店铺标题" disabled clearable />
              <el-button
                type="text"
                size="small"
                @click="showJoinSearch({
                searchDialog: 'GoodsShopMain'
              })"
              >选择店铺标题</el-button>
              <el-button
                type="text"
                size="small"
                @click="form.shopId = 0; form.shopTitle = '无'; form.shopIsActive = 1;"
              >设为公共模板</el-button>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="模板标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入模板标题" clearable />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="发货地" prop="expressPlace">
              <el-input v-model="form.expressPlace" placeholder="请输入发货地" clearable />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="是否包邮" prop="expressFree">
              <el-select
                v-model="form.expressFree"
                placeholder="请选择是否包邮"
                clearable
                style="width: 100%;"
              >
                <el-option
                  v-for="item in selects.expressFreeSelect"
                  :value="item.value"
                  :key="item.value"
                  :label="item.displayValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="计价方式" prop="valuationType">
              <el-select
                v-model="form.valuationType"
                placeholder="请选择计价方式"
                clearable
                style="width: 100%;"
              >
                <el-option
                  v-for="item in selects.valuationTypeSelect"
                  :value="item.value"
                  :key="item.value"
                  :label="item.displayValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="默认快递费" prop="defaultFee">
              <el-input-number
                v-model="form.defaultFee"
                placeholder="请输入默认快递费"
                clearable
                style="width: 100%;"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="默认规格" prop="defaultSpec">
              <el-input-number
                v-model="form.defaultSpec"
                placeholder="请输入默认递增规格"
                clearable
                style="width: 100%;"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="默认递增规格" prop="defaultIncrementSpec">
              <el-input-number
                v-model="form.defaultIncrementSpec"
                placeholder="请输入默认递增规格"
                clearable
                style="width: 100%;"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="默认递增快递费" prop="defaultIncrementFee">
              <el-input-number
                v-model="form.defaultIncrementFee"
                placeholder="请输入默认递增快递费"
                clearable
                style="width: 100%;"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="默认包邮条件" prop="defaultFreeCondition">
              <el-input-number
                v-model="form.defaultFreeCondition"
                placeholder="请输入默认包邮条件"
                clearable
                style="width: 100%;"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="默认包邮金额" prop="defaultFreeMaxPayment">
              <el-input-number
                v-model="form.defaultFreeMaxPayment"
                placeholder="请输入默认包邮金额"
                clearable
                style="width: 100%;"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <el-form-item label="模板说明" prop="description">
              <el-input type="textarea" v-model="form.description" placeholder="请输入模板说明" clearable />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="是否默认模板" prop="isDefault">
              <el-select
                v-model="form.isDefault"
                placeholder="请选择是否默认模板"
                clearable
                style="width: 100%;"
              >
                <el-option
                  v-for="item in selects.isDefaultSelect"
                  :value="item.value"
                  :key="item.value"
                  :label="item.displayValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button @click="dialog.add = false">取消</el-button>
        <el-button type="primary" @click="add" :loading="loading.add">添加</el-button>
      </div>
    </el-dialog>
    <el-dialog
      :visible.sync="dialog.edit"
      :close-on-click-modal="false"
      title="修改"
      width="80%"
      append-to-body
      @closed="dialogClosed('editForm')"
    >
      <el-form ref="editForm" :model="form" label-width="120px" :rules="validateRules">
        <el-row>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="店铺标题" prop="shopTitle">
              <el-input v-model="form.shopTitle" placeholder="请输入店铺标题" disabled clearable />
              <el-button
                type="text"
                size="small"
                @click="showJoinSearch({
                searchDialog: 'GoodsShopMain'
              })"
              >选择店铺标题</el-button>
              <el-button
                type="text"
                size="small"
                @click="form.shopId = 0; form.shopTitle = '无'; form.shopIsActive = 1;"
              >设为公共模板</el-button>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="模板标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入模板标题" clearable />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="发货地" prop="expressPlace">
              <el-input v-model="form.expressPlace" placeholder="请输入发货地" clearable />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="是否包邮" prop="expressFree">
              <el-select
                v-model="form.expressFree"
                placeholder="请选择是否包邮"
                clearable
                style="width: 100%;"
              >
                <el-option
                  v-for="item in selects.expressFreeSelect"
                  :value="item.value"
                  :key="item.value"
                  :label="item.displayValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="计价方式" prop="valuationType">
              <el-select
                v-model="form.valuationType"
                placeholder="请选择计价方式"
                clearable
                style="width: 100%;"
              >
                <el-option
                  v-for="item in selects.valuationTypeSelect"
                  :value="item.value"
                  :key="item.value"
                  :label="item.displayValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="默认快递费" prop="defaultFee">
              <el-input-number
                v-model="form.defaultFee"
                placeholder="请输入默认快递费"
                clearable
                style="width: 100%;"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="默认规格" prop="defaultSpec">
              <el-input-number
                v-model="form.defaultSpec"
                placeholder="请输入默认递增规格"
                clearable
                style="width: 100%;"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="默认递增规格" prop="defaultIncrementSpec">
              <el-input-number
                v-model="form.defaultIncrementSpec"
                placeholder="请输入默认递增规格"
                clearable
                style="width: 100%;"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="默认递增快递费" prop="defaultIncrementFee">
              <el-input-number
                v-model="form.defaultIncrementFee"
                placeholder="请输入默认递增快递费"
                clearable
                style="width: 100%;"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="默认包邮条件" prop="defaultFreeCondition">
              <el-input-number
                v-model="form.defaultFreeCondition"
                placeholder="请输入默认包邮条件"
                clearable
                style="width: 100%;"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="默认包邮金额" prop="defaultFreeMaxPayment">
              <el-input-number
                v-model="form.defaultFreeMaxPayment"
                placeholder="请输入默认包邮金额"
                clearable
                style="width: 100%;"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <el-form-item label="模板说明" prop="description">
              <el-input type="textarea" v-model="form.description" placeholder="请输入模板说明" clearable />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="是否默认模板" prop="isDefault">
              <el-select
                v-model="form.isDefault"
                placeholder="请选择是否默认模板"
                clearable
                style="width: 100%;"
              >
                <el-option
                  v-for="item in selects.isDefaultSelect"
                  :value="item.value"
                  :key="item.value"
                  :label="item.displayValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button @click="dialog.edit = false">取消</el-button>
        <el-button type="primary" @click="edit" :loading="loading.edit">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as utils from '@/utils/utils'
import * as ResponseStatus from '@/utils/response-status'
import UploadImage from '@/components/Upload/UploadImage'
import Tinymce from '@/components/Tinymce'
const resUrl = process.env.VUE_APP_RES_URL
export default {
  name: 'ExpressTemplateAddEdit',
  components: {
    UploadImage,
    Tinymce,
  },
  data() {
    return {
      dialog: {
        add: false,
        edit: false,
      },
      loading: {
        add: false,
        edit: false,
      },
      form: {
        shopId: undefined,
        shopTitle: undefined,
        shopIsActive: undefined,
        title: undefined,
        expressPlace: undefined,
        expressFree: undefined,
        valuationType: undefined,
        defaultFee: undefined,
        defaultSpec: undefined,
        defaultIncrementSpec: undefined,
        defaultIncrementFee: undefined,
        defaultFreeCondition: undefined,
        defaultFreeMaxPayment: undefined,
        description: undefined,
        isDefault: undefined,
      },
      validateRules: {
        shopId: [
          {
            type: 'integer',
            required: true,
            message: '此项为必须项',
            trigger: 'blur, change',
          },
        ],
        shopTitle: [
          {
            type: 'string',
            required: true,
            message: '此项为必须项',
            trigger: 'blur',
          },
          {
            type: 'string',
            min: 1,
            max: 50,
            message: '必须1-50个字符',
            trigger: 'blur',
          },
        ],
        shopIsActive: [
          {
            type: 'integer',
            required: true,
            message: '此项为必须项',
            trigger: 'blur, change',
          },
        ],
        title: [
          {
            type: 'string',
            required: true,
            message: '此项为必须项',
            trigger: 'blur',
          },
          {
            type: 'string',
            min: 1,
            max: 50,
            message: '必须1-50个字符',
            trigger: 'blur',
          },
        ],
        expressPlace: [
          {
            type: 'string',
            required: true,
            message: '此项为必须项',
            trigger: 'blur',
          },
          {
            type: 'string',
            min: 1,
            max: 50,
            message: '必须1-50个字符',
            trigger: 'blur',
          },
        ],
        expressFree: [
          {
            type: 'integer',
            required: true,
            message: '此项为必须项',
            trigger: 'blur, change',
          },
        ],
        defaultFee: [
          {
            type: 'number',
            required: true,
            message: '此项为必须项',
            trigger: 'blur, change',
          },
        ],
        defaultSpec: [
          {
            type: 'number',
            required: true,
            message: '此项为必须项',
            trigger: 'blur, change',
          },
        ],
        description: [
          {
            type: 'string',
            min: 1,
            max: 500,
            message: '必须1-500个字符',
            trigger: 'blur',
          },
        ],
      },
      dataDictCodes: ['YES_NO', 'VALUATION_TYPE'],
      selects: {
        shopIsActiveSelect: [],
        expressFreeSelect: [],
        valuationTypeSelect: [],
        defaultSpecSelect: [],
        isDefaultSelect: [],
      },
      resUrl: resUrl,
    }
  },
  computed: {},
  mounted() {
    this.initDataDict()
  },
  methods: {
    initDataDict() {
      utils
        .getDataDict(this.dataDictCodes)
        .then((response) => {
          if (response) {
            utils.getSelectDictItems({
              mainComponent: this,
              dataDictItems: response,
              dictCode: 'YES_NO',
              formProp: 'shopIsActive',
              dataProp: 'shopIsActiveSelect',
            })
            utils.getSelectDictItems({
              mainComponent: this,
              dataDictItems: response,
              dictCode: 'YES_NO',
              formProp: 'expressFree',
              dataProp: 'expressFreeSelect',
            })
            utils.getSelectDictItems({
              mainComponent: this,
              dataDictItems: response,
              dictCode: 'VALUATION_TYPE',
              formProp: 'valuationType',
              dataProp: 'valuationTypeSelect',
            })
            utils.getSelectDictItems({
              mainComponent: this,
              dataDictItems: response,
              dictCode: 'YES_NO',
              formProp: 'isDefault',
              dataProp: 'isDefaultSelect',
            })
          }
        })
        .catch((error) => {})
    },
    dialogClosed(formRef) {
      this.$refs[formRef].resetFields()
      for (let key in this.form) {
        this.form[key] = undefined
      }
    },
    add() {
      this.$emit('add')
    },
    edit() {
      this.$emit('edit')
    },
    /** 触发父组件展示关联表数据的搜索对话框 */
    showJoinSearch(options) {
      this.$emit('showJoinSearchDialog', options)
    },
    imgUploadSuccess(formProp, url) {
      this.form[formProp] = url
    },
    imgRemoveSuccess(formProp) {
      this.form[formProp] = undefined
    },
  },
}
</script>

<style>
</style>